<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 药品销售
                </el-breadcrumb-item>
                <el-breadcrumb-item>销售信息</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.name" placeholder="请输入病人姓名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="patientBillId" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="patientName" label="病人姓名"></el-table-column>
                <el-table-column prop="patientPhone" label="病人手机"></el-table-column>
                <el-table-column prop="doctorName" label="销售人员"></el-table-column>
                <el-table-column prop="allQuantity" label="药品总量"></el-table-column>
                <el-table-column prop="allPrice" label="药品总价"></el-table-column>
                <el-table-column prop="state" label="状态码"></el-table-column>

                <!-- <el-table-column prop="createTime" label="添加时间"></el-table-column> -->
                <!-- <el-table-column prop="updateTime" label="修改时间"></el-table-column> -->
                <el-table-column label="操作" width="120" align="center">
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-more" @click="handleDetail(scope.$index, scope.row)">详情
                        </el-button>
<!--                        <el-button type="text" icon="el-icon-delete" class="red"-->
<!--                            @click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
                    </template>
                </el-table-column>
                <el-table-column label="是否取药" width="130" align="center" v-if="modifyTag">
                    <template #default="scope">
                        

                        <el-button v-if="scope.row.state === 0" type="text" icon="el-icon-edit" @click="handleYes(scope.$index, scope.row)">
                            Yes
                        </el-button>

                         <el-button v-if="scope.row.state === 0" type="text" icon="el-icon-delete" class="red" @click="handleNo(scope.$index, scope.row)">
                             No
                        </el-button>

                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.current"
                    :page-size="query.pageSize" :total="query.total" @current-change="handlePageChange"></el-pagination>
            </div>
        </div>

        <!-- 详情弹出框 -->
        <el-dialog title="详情" v-model="detailVisible" width="30%">
            <el-table :data="detailData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
                <el-table-column prop="patientBillIntroId" label="编号" width="50" align="center"></el-table-column>
                <el-table-column prop="medicineName" label="药品名称"></el-table-column>
                <el-table-column prop="medicineQuantity" label="药品数量"></el-table-column>
                <el-table-column prop="medicinePrice" label="药品单价"></el-table-column>
            </el-table>
            <template #footer>
                <span class="dialog-footer">
                    <el-button type="primary" @click="detailVisible = false">关 闭</el-button>
                </span>
            </template>
        </el-dialog>


    </div>
</template>

<script>
import { onActivated, ref, reactive, inject } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getPatientBillList, getPatientBillDetail,outStock, cancelStock } from "../../api";

export default {
    name: "patientBill",
    setup() {
        const query = reactive({
            current: 1,
            pageSize: 10,
            total: 0,
            name: null
        });
        const tableData = ref([]);
        const pageTotal = ref(0);
        const reload  = inject("reload");
        let modifyTag = true;
        const initTag = () => {
            let role = localStorage.getItem("role");
            if (role === 1 || role === '1' || role === 2 || role === '2') {
                modifyTag = false;
            }
        };
        initTag();
        // 获取表格数据
        const getData = () => {
            getPatientBillList(query).then((res) => {
                tableData.value = res.data.message;
                query.total = res.data.total;
            });
        };
        getData();

        // 查询操作
        const handleSearch = () => {
            query.current = 1;
            getData();
        };
        // 分页导航
        const handlePageChange = (val) => {
            query.current = val;
            getData();
        };

        // 确认取药操作
        const handleYes = (index, row) => {
            // 二次确认
            ElMessageBox.confirm("确定已取药吗？", "提示", {
                type: "warning",
            }).then(() => {
                console.log(row.patientBillId);
                outStock(row.patientBillId).then(res => {
                    if (res.code === 200) {
                        reload();
                        ElMessage.success('已取药');
                    } else {
                        console.log("[操作] 失败: " + res.message);
                        ElMessage.error('服务器繁忙，请稍后重试');
                    }
                })
            }).catch(() => {});
        };

        // 取消取药操作
        const handleNo = (index, row) => {
            // 二次确认
            ElMessageBox.confirm("确定取消药单吗？", "提示", {
                type: "warning",
            }).then(() => {
                console.log(row.patientBillId);
                cancelStock(row.patientBillId).then(res => {
                    if (res.code === 200) {
                        reload();
                        ElMessage.success('已取消');
                    } else {
                        console.log("[操作] 失败: " + res.message);
                        ElMessage.error('服务器繁忙，请稍后重试');
                    }
                })
            }).catch(() => {});
        };


        // 详情表格弹窗
        const detailVisible = ref(false);
        let detailData = ref([]);

        const handleDetail = (index, row) => {
            console.log("[PatientBill] handleDetail");
            getPatientBillDetail(row.patientBillId).then(res => {
                if (res.code === 200) {
                    detailData.value = res.data.patientBillIntro;
                    detailVisible.value = true;
                } else {
                    console.log("[获取销售详情] 失败: " + res.message);
                    ElMessage.error('服务器繁忙，请稍后重试');
                }
            })
        };

        onActivated(() => {
            console.log("PatientBill onActivated!");
            getData();
        });

        return {
            query,
            tableData,
            pageTotal,
            detailVisible,
            modifyTag,
            detailData,
            handleSearch,
            handlePageChange,
            //handleDelete,
            handleYes,
            handleNo,
            handleDetail,
        };
    },
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
</style>
